package org.example;

// enum status//棋子颜色枚举类
// {
//     BLACK, WHITE,EMPTY
// }
class Border//棋盘类
{
    private int[][] chessboard;//(0为空，1为黑，2为白)
    private int size = 15;

    public Border()
    {
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            {
                chessboard[i][j] = 0; //初始化棋盘为0
            }
        }
    }

    public int getSize()
    {
        return size;
    }
    public void setSize(int size)
    {
        this.size = size;
    }

    public int[][] getChessboard()
    {
        return chessboard;
    }
    public void setChessboard(int[][] chessboard)
    {
        this.chessboard = chessboard;
    }

    //检查落子是否合法
    public boolean checkMove(int x, int y)
    {

        if(this.chessboard[x][y]== 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //落子
    public void move(int x, int y, Player player)
    {
        //获取鼠标坐标x，y
        if(checkMove(x, y))
        {
            this.chessboard[x][y] = player.getColor();
        }
    }

    // 撤销落子
    public void undoMove(int x, int y) {
        this.chessboard[x][y] = 0;
    }

    // 判断游戏是否结束
    public boolean isGameOver() {
        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                if (chessboard[x][y] != 0) {
                    Player player = new Player(chessboard[x][y]);
                    if (isWin(x, y, player)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    //判断是否获胜
    public boolean isWin(int x, int y, Player player)
    {
        for(int i=1;i<5;i++)
        {
            switch (i){
                case 1:
                    //垂直方向
                    //正向
                    int count1 = 1;
                    int newY1 = y+1;
                    while(newY1<size && chessboard[x][newY1]==player.getColor())
                    {
                        count1++;
                        newY1++;
                    }
                    //反向
                    if(count1<5)
                    {
                        int newY2 = y-1;
                        while(newY1>=0 && chessboard[x][newY2]==player.getColor())
                        {
                            count1++;
                            newY2--;
                        }
                    }
                    if(count1>=5)
                        return true;
                    else
                        return false;
                case 2:
                    //水平方向
                    //正向
                    int count2 = 1;
                    int newX1 = x+1;
                    while(newX1<size && chessboard[newX1][y]==player.getColor())
                    {
                        count2++;
                        newX1++;
                    }
                    //反向
                    if(count2<5)
                    {
                        int newX2 = x-1;
                        while(newX2>=0 && chessboard[newX2][y]==player.getColor())
                        {
                            count2++;
                            newX2--;
                        }
                    }
                    if(count2>=5)
                        return true;
                    else
                        return false;
                case 3:
                    //斜向右上
                    //正向
                    int count3 = 1;
                    int newrightX1 = x+1;
                    int newrightY1 = y+1;
                    while(newrightX1<size && newrightY1<size && chessboard[newrightX1][newrightY1]==player.getColor())
                    {
                        count3++;
                        newrightX1++;
                        newrightY1++;
                    }
                    //反向
                    if(count3<5)
                    {
                        int newrightX2 = x-1;
                        int newrightY2 = y-1;
                        while(newrightX2>=0 && newrightY2>=0 && chessboard[newrightX2][newrightY2]==player.getColor())
                        {
                            count3++;
                            newrightX2--;
                            newrightY2--;
                        }
                    }
                    if(count3>=5)
                        return true;
                    else
                        return false;
                case 4:
                    //斜向左上
                    //正向
                    int count4 = 1;
                    int newleftX1 = x+1;
                    int newleftY1 = y-1;
                    while(newleftX1<size && newleftY1>=0 && chessboard[newleftX1][newleftY1]==player.getColor())
                    {
                        count4++;
                        newleftX1++;
                        newleftY1--;
                    }
                    //反向
                    if(count4<5)
                    {
                        int newleftX2 = x-1;
                        int newleftY2 = y+1;
                        while(newleftX2>=0 && newleftY2<size && chessboard[newleftX2][newleftY2]==player.getColor())
                        {
                            count4++;
                            newleftX2--;
                            newleftY2++;
                        }
                    }
                    if(count4>=5)
                        return true;
                    else
                        return false;
            }
        }
        return false;
    }

}
